package com.li.youhappygo.controller;

import com.github.pagehelper.PageInfo;
import com.li.youhappygo.annotation.Log;
import com.li.youhappygo.annotation.OperatorType;
import com.li.youhappygo.model.dto.CategoryQueryParamDto;
import com.li.youhappygo.model.entity.commond.Category;
import com.li.youhappygo.model.vo.category.CategoryVo;
import com.li.youhappygo.model.vo.common.Result;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.service.annotation.PostExchange;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Tag(name = "分类管理", description = "分类管理")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @GetMapping("/getCategoryList/{pageNum}/{pageSize}")
    @Operation(summary = "查询所有分类",description = "直接查询所有的分类")
    public Result<PageInfo<CategoryVo>> getCategoryList(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        System.out.println("pageNum:"+pageNum+"pageSize:"+pageSize);
        PageInfo<CategoryVo> pageInfo = categoryService.getCategoryList(pageNum,pageSize);
        return Result.build(pageInfo, ResultCode.SUCCESS);
    }
    @GetMapping("getChildCategory/{categoryId}")
    @Operation(summary = "根据父分类ID查询子分类",description = "根据父分类ID查询子分类")
    public Result<List<Category>> getChildCategory(@PathVariable Long categoryId) {
        List<Category> categoryList = categoryService.getChildCategory(categoryId);
        return Result.build(categoryList,ResultCode.SUCCESS);
    }

    @PostMapping("/insertCategory")
    @Operation(summary = "插入分类",description = "插入一个分类信息")
    @Log(title = "插入分类", businessType = 1,operatorType = OperatorType.MANAGE)
    public Result insertCategory(@RequestBody Category category) {
        categoryService.insertCategory(category);
        return Result.build(null, ResultCode.SUCCESS);
    }

    @PostMapping("updateCategory")
    @Operation(summary = "更新分类",description = "更新一个分类信息")
    @Log(title = "更新分类", businessType = 2,operatorType = OperatorType.MANAGE)
    public Result updateCategory(@RequestBody Category category) {
        categoryService.updateCategory(category);
        return Result.build(null, ResultCode.SUCCESS);
    }

    @GetMapping("/deleteCategory")
    @Operation(summary = "删除分类",description = "根据ID删除一个分类")
    @Log(title = "删除分类", businessType = 3,operatorType = OperatorType.MANAGE)
    public Result deleteCategory(@RequestParam(name = "id") Long id) {
        categoryService.deleteCategory(id);
        return Result.build(null, ResultCode.SUCCESS);
    }

    @PostMapping("getCategoryListByQueryParam")
    @Operation(summary = "根据查询条件查询分类",description = "根据查询条件查询分类")
    public Result<PageInfo<CategoryVo>> getCategoryListByQueryParam(@RequestBody CategoryQueryParamDto categoryQueryParamDto,
                                                                    @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageInfo<CategoryVo> pageInfo = categoryService.getCategoryListByQueryParam(categoryQueryParamDto,pageNum,pageSize);
        return Result.build(pageInfo, ResultCode.SUCCESS);
    }
}
